package com.itheima.service;


import cn.hutool.core.collection.CollectionUtil;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> getMenuByUserName(String username) {
        List<Menu> allMenu = menuMapper.getMenuByUserName(username);
        List<Menu> rootMenu = new ArrayList<>();
        for (Menu menu : allMenu) {
            if (menu.getParentMenuId() == null) {
                rootMenu.add(menu);
            }
        }
        for (Menu menu : rootMenu) {
            List<Menu> children = getChild(menu.getId(),allMenu);
            menu.setChildren(children);
        }
        return rootMenu;
    }

    @Override
    public void add(Menu menu) {
        menuMapper.add(menu);
    }

    @Override
    public void edit(Menu menu) {
        menuMapper.edit(menu);
    }



    /**
     * 递归父子排序
     * @param parentList
     * @param queryList
     */
    void findParent(List<Menu> parentList,List<Menu> queryList){
        List<Menu> menuList = new ArrayList<>();
        for (Menu menu : parentList) {
            Integer id = menu.getId();
            for (Menu menu1 : queryList) {
                Integer parentMenuId = menu1.getParentMenuId();
                if(id == parentMenuId){
                    menu.getChildren().add(menu1);
                }
            }
            if(CollectionUtil.isNotEmpty(menu.getChildren())){
                findParent(menu.getChildren(),queryList);
            }
        }

    }

    /**
     * 递归设置ID
     * @param menuList
     */
    private void setId(List<Menu> menuList){
        int id = 0;
        for (Menu menu : menuList) {
            id++;
            menu.setShowId(id);
            if(CollectionUtil.isNotEmpty(menu.getChildren())){
                setId(menu.getChildren());
            }
        }
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
//        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        int count = menuMapper.findCount();
        List<Menu> list = menuMapper.findAll(queryPageBean.getQueryString());
        List<Menu> returnmenu = new ArrayList<>();
        for (Menu menu : list) {
            Integer parentMenuId = menu.getParentMenuId();
            if(parentMenuId == null){
                returnmenu.add(menu);
            }
        }
        //递归操作
        findParent(returnmenu,list);
        setId(returnmenu);
        long con = count + 0L;
        return new PageResult(con ,returnmenu);

    }

    @Override
    public void deleteById(int id) {
        menuMapper.deleteById(id);
    }

    @Override
    public Menu findById(int id) {
        return menuMapper.findById(id);
    }

    //递归查询
    public List<Menu> getChild(Integer id, List<Menu> menus) {
        List<Menu> children = new ArrayList<>();

        for (Menu menu : menus) {
            if (id.equals(menu.getParentMenuId())) {
                children.add(menu);
            }
        }
        if (children.size() == 0) {
            return children;
        }
        for (Menu child : children) {
            List<Menu> childchildren = getChild(child.getId(), menus);
            child.setChildren(childchildren);
        }

        return children;
    }


}
